package uk.ac.lkl.lpp.model.xml;



import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import uk.ac.lkl.common.util.ds.NotifyingList;
import uk.ac.lkl.lpp.model.PedagogyPlanModel;
import uk.ac.lkl.lpp.model.ldo.CognitiveActivity;


public class CognitiveActivitySetProcessor extends ModelElementProcessor {

    public CognitiveActivitySetProcessor() {
        super("CognitiveActivitySet");
    }


    public void convertDataToElements(PedagogyPlanModel plan,
                                      Document document,
                                      Element pedagogyPlanElement) {
        // should be done in base class
        Element parentElement = document.createElement("CognitiveActivitySet");
        pedagogyPlanElement.appendChild(parentElement);

        NotifyingList<CognitiveActivity> list = (NotifyingList<CognitiveActivity>) plan
                .getList(CognitiveActivity.class);
        for (int i = 0; i < list.size(); i++) {
            CognitiveActivity cognitiveActivity = list.get(i);
            Element childElement = document.createElement("CognitiveActivity");
            childElement.setAttribute("index", Integer.toString(i));

            String title = cognitiveActivity.getTitle();
            childElement.setAttribute("title", title);

            String description = cognitiveActivity.getDescription();
            childElement.setTextContent(description);

            String link = cognitiveActivity.getLink();
            childElement.setAttribute("link", link);

            parentElement.appendChild(childElement);
        }
    }


    public void convertElementToData(PedagogyPlanModel plan, Element element) {
        NotifyingList<CognitiveActivity> list = new NotifyingList<CognitiveActivity>(
                CognitiveActivity.class);
        NodeList nodes = element.getElementsByTagName("CognitiveActivity");
        for (int i = 0; i < nodes.getLength(); i++) {
            Element childElement = (Element) nodes.item(i);
            String title = childElement.getAttribute("title");
            String description = childElement.getTextContent();
            CognitiveActivity cognitiveActivity = new CognitiveActivity(title,
                    description);

            String link = childElement.getAttribute("link");
            cognitiveActivity.setLink(link);

            list.add(cognitiveActivity);
        }
        plan.addList(CognitiveActivity.class, list);
    }

}
